package com.hopu.controller;

import com.hopu.domain.*;
import com.hopu.service.ContractCService;
import com.hopu.service.ContractProductCService;
import com.hopu.service.ContractVOService;
import com.hopu.service.ExtCproductCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/contract")
public class ContractController {

    @Autowired
    ContractCService contractCService;

    @Autowired
    ContractProductCService productCService;

    @Autowired
    ExtCproductCService extCproductCService;

    @Autowired
    ContractVOService contractVOService;






    @RequestMapping("/findContract")
    public String findContract(Model model){
        List<ContractC> contract = contractCService.selectByExample();

        model.addAttribute("dataList",contract);
        return "/basicinfo/contract/jContractList";
    }

    /*
    * 未完成
    * */
    @RequestMapping("/toview")
    public String toview(String contractId,Model model){
        ContractC contract = contractCService.selectByPrimaryKey(contractId);
        model.addAttribute("obj",contract);
        /*第二部分*/
        ContractVO view = contractVOService.view(contractId);
            List<ContractProductCVo> contractProductcS = view.getContractProductcS();
        System.out.println("contractProductcS.size() = :" + contractProductcS.size());
            model.addAttribute("contractProductcS",contractProductcS);
            /*List<ExtCproductCVo> list = new ArrayList<>();*/

            /*for (ContractProductCVo contractProductc : contractProductcS) {
                List<ExtCproductCVo> extCproductCvos = contractProductc.getExtCproductCVos();
                for (ExtCproductCVo extCproductCvo : extCproductCvos) {
                    System.out.println(extCproductCvo);
                    list.add(extCproductCvo);
                }

            }*/
       /* for (ExtCproductCVo extCproductCvo : list) {
            model.addAttribute("extCproductCVos",list);
            model.addAttribute("num",0);
        }
*/




        return "/basicinfo/contract/jContractView";
    }

    @RequestMapping("/test")
    @ResponseBody
    public  ContractVO toview(){
        /*第二部分*/
        ContractVO view = contractVOService.view("de3bc9e1d7c04ab980795779c488eea7");
        return view;
    }

    /**
     * 去添加页面
     * @return
     */
    @RequestMapping("/tocreate")
    public String tocreate(){
        return "/basicinfo/contract/jContractCreate";
    }

    @RequestMapping("/insert")
    public String insert(ContractC contract){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        contract.setContractId(uuid);
        contract.setState(0);
        contract.setOutState(0);
        contract.setOldState(0);
        int i = contractCService.insertSelective(contract);
        return "redirect:findContract";
    }

    /**
     * 去修改页面
     * @param model
     * @param contractId
     * @return
     */
    @RequestMapping("/toupdate")
    public String toupdate(Model model,String contractId){
        ContractC contract = contractCService.selectByPrimaryKey(contractId);
        model.addAttribute("obj",contract);
        return "/basicinfo/contract/jContractUpdate";
    }
    @RequestMapping("/update")
    public String update(ContractC contract){
        contract.setState(0);
        contract.setOutState(0);
        contract.setOldState(0);
        contractCService.updateByPrimaryKey(contract);
        return "redirect:findContract";
    }

    @RequestMapping("/submit")
    public String submit(String[] contractId,Map<String, Object> map){
        if (contractId != null) {
            for (String fid : contractId) {
                map.put("contractId",fid);
                Integer i = 1;
                map.put("state",i);
                contractCService.state(map);
            }
        }
        return "redirect:findContract";
    }

    @RequestMapping("/cancel")
    public String cancel(String[] contractId,Map<String, Object> map){
        if (contractId != null) {
            for (String fid : contractId) {
                map.put("contractId",fid);
                Integer i = 0;
                map.put("state",i);
                contractCService.state(map);
            }
        }
        return "redirect:findContract";
    }

    @RequestMapping("/delete")
    public String delete(String[] contractId) {
        //我先根据合同id查所有货物id
        //然后根据所有货物id删除附件
        //在删除合同下的所有货物
        //最后删除合同
        if (contractId != null) {
            for (String fid : contractId) {
                //1.
                List<ContractProductC> contractProductCS = productCService.selectByContractProductC(fid);
                System.out.println("我是级联删除第一步:"+contractProductCS.size());
                //2.1
                //新建个数组
                String[] contractProductId = new String[contractProductCS.size()];

                for (ContractProductC contractProductC : contractProductCS) {
                    //得到这个合同下所有的货物id
                    for (int num =0;num<contractProductId.length;num++){
                        //把得到的货物id放到新建的数组
                        contractProductId[num]=contractProductC.getContractProductId();
                    }
                }
                //2.2
                int i = extCproductCService.deleteByContractProductById(contractProductId);
                if (i>0){
                    //3.
                    int i1 = productCService.deleteByContractId(contractId);
                    if (i1>0){
                        int i2 = contractCService.deleteByPrimaryKey(fid);
                    }
                }

            }
        }
        return "redirect:findContract";
    }




}
